#!/bin/bash

DAEMON="/sbin/spindownd"
CONFPATH="/etc/spindown.conf"
VARDIR="/var/run/spindown"
FIFOPATH=$VARDIR"/spindownd.fifo"
PIDFILE=$VARDIR"/spindownd.pid"

. /lib/lsb/init-functions

#prints table with status about the disks
status()
{
    #print the header of the table
    printf "%-5s %10s %10s %15s %20s\n" name watched active idle-time spindown-time

    while read line; do

            printf "%-5s %10s %10s %15s %20s\n" $line

    done < $FIFOPATH

    exit 0
}

case "$1" in
    "start")
        log_daemon_msg "Starting disk spindown daemon" "spindownd"
        mkdir $VARDIR &> /dev/null
        start_daemon -p $PIDFILE $DAEMON -d -f $FIFOPATH  -c $CONFPATH -p $PIDFILE
        log_end_msg $?

        exit $?
        ;;

    "stop")
        log_daemon_msg "Stopping disk spindown daemon" "spindownd"
        killproc -p $PIDFILE $DAEMON
        rmdir $VARDIR &> /dev/null
        log_end_msg $?

        exit $?
        ;;

    "status")
        if status_of_proc -p $PIDFILE $DAEMON spindown; then
            echo -n
        else
            exit 1
        fi

        # First open the fifo and then send the signal
        status &
        sleep 0.1
        killproc -p $PIDFILE $DAEMON -PIPE
        wait $!

        exit 0
        ;;

    "restart"|"condrestart")
        $0 stop
        $0 start

        exit $?
        ;;

    "reload")
        log_daemon_msg "Reloading disk spindown daemon" "spindownd"
        killproc -p $PIDFILE $DAEMON -HUP
        log_end_msg $?

        exit $?
        ;;

    *)
        echo "Usage: $0 {start|stop|restart|reload|status}"
        exit 1
esac

exit 0
